package cn.com.spring.model;

import lombok.Data;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 注释链
 *
 * @author LiuGuodong
 * @date 2024/02/02
 */
@Data
public class AnnotationChain {
    private Object aspect;
    private EntityInvocation entityInvocation;
    private Method methodProxy;

    public AnnotationChain(Object aspect, EntityInvocation entityInvocation, Method methodProxy) {
        this.aspect = aspect;
        this.entityInvocation = entityInvocation;
        this.methodProxy = methodProxy;
    }

    public Object run() throws InvocationTargetException, IllegalAccessException {
        if (methodProxy != null) {
            // 校验是否为切面方法
            Class<?>[] parameterTypes = methodProxy.getParameterTypes();
            if (parameterTypes.length == 1 && EntityInvocation.class.equals(parameterTypes[0])) {
                return methodProxy.invoke(aspect, entityInvocation);
            } else {
                return methodProxy.invoke(aspect);
            }
        }
        return null;
    }
}
